package my.peerencrypt;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

import my.peerencrypt.dao.UserDao;

public class ServerThread extends Thread {

	private Socket socket;
	private UserDao userDao;
	
	public ServerThread(Socket socket){
		this.socket = socket;
		this.userDao = new UserDao();
	}
	
	public void run(){
		try {
			DataInputStream din = new DataInputStream(socket.getInputStream());
			DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
			int cmd = din.readInt();
			if(cmd==1){//login
				String userName = readString(din);
				String password = readString(din);
				//密文
				String ec = readString(din);
				User u = userDao.getUserByUserName(userName);
				if(userName==null || password==null || ec == null || u==null || 
						!u.isHasRegister() || !u.getPassword().equals(password)){
					sendResult(dout,1);//
				}else{
					int[] ecarr = Encrypt.strToIntArr(ec);//密文
					int[] key = new int[11];
					String[] ss = u.getKey().split("\\|");
					for(int i=0;i<11;i++){
						key[i] = Integer.valueOf(ss[i]);
					}
					String src = Encrypt.decrypt(ecarr, key);
					if(src.equals(u.getRegistrandom())){
						sendResult(dout,0);//
					}else{
						sendResult(dout,1);//
					}
				}
			}else if(cmd==2){//注册
				String userName = readString(din);
				String password = readString(din);
				
				String registrandom = readString(din);
				String key = readString(din);
				
				User u = userDao.getUserByUserName(userName);
				if(userName==null || password==null || registrandom == null || key==null || u==null || 
						u.isHasRegister() || !u.getPassword().equals(password)){
					sendResult(dout,3);//注册失败
					System.out.println("server 注册失败");
				}else{
					u.setRegistrandom(registrandom);
					u.setKey(key);
					userDao.registUser(u);
					sendResult(dout,2);//注册成功
					System.out.println("server 注册成功");
					
					}
				}
			
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
				//socket.shutdownInput();
				//socket.shutdownOutput();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	
	/**
	 * @param dout
	 * @param r 1登录失败 0登录成功
	 * @throws IOException
	 */
	public void sendResult(DataOutputStream dout,int r) throws IOException{
		dout.writeInt(r);
		dout.flush();
	}
	
	public String readString(DataInputStream din) throws IOException{
		for(int i=0;i<10;i++){
			if(din.available()<4){
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}else{
				break;
			}
		}
		
		int length = din.readInt();
		byte[] b = new byte[length];
		for(int i=0;i<10;i++){
			if(din.available()<length){
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				continue;
			}else{
				break;
			}
		}
		int l = din.read(b);
		if(l!=length){
			return null;
		}else{
			return new String(b);
		}
	}
}
